package com.css.ids.server.service.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.css.apps.base.common.Constants;
import com.css.apps.base.org.model.SOrg;
import com.css.db.query.QueryCache;
import com.css.ids.server.service.IdsService;
import com.css.util.ListUtil;
import com.css.util.StringHelper;

public class IdsOrgDao {
	
    public static SOrg getOrg(String orgId) {
        return validateOrg(QueryCache.get(SOrg.class, orgId));
    }
	
	@SuppressWarnings("unchecked")
	public static List<SOrg> listOrgs(Collection<String> orgIds) {
		List<SOrg> orgs = QueryCache.idToObj(SOrg.class, new ArrayList<>(orgIds));
        return validateOrg(orgs);
    }

	public static List<String> listAllOrgId() {
		return listAllOrgId(0, null);
	}
	
	public static List<SOrg> listAllOrg() {
        return listAllOrg(0, null);
    }

	public static List<SOrg> listAllOrg(int orgLevel) {
		return listAllOrg(orgLevel, null);
	}
	
	public static List<String> listAllOrgId(int orgLevel, String orgCode) {
		return listAllOrgId(orgLevel, orgCode, null);
	}
	
    public static List<SOrg> listAllOrg(int orgLevel, String orgCode) {
    	return listAllOrg(orgLevel, orgCode, null);
    }
    
    @SuppressWarnings("unchecked")
	public static List<String> listAllOrgId(int orgLevel, String orgId, String orgType) {
    	List<String> orgTypeList = new ArrayList<>();
    	if (orgLevel < 0) {
    		orgLevel = 1;
    	}
    	if (StringHelper.isEmpty(orgType) || IdsService.ALL_ORG_TYPE.equals(orgType)) {
    		orgTypeList.add(IdsService.DEPT_ORG_TYPE);
    		orgTypeList.add(IdsService.ORG_ORG_TYPE);
    	}
    	if (IdsService.ORG_ORG_TYPE.equals(orgType)) {
    		if (StringHelper.isEmpty(orgId)) {
    			return new QueryCache("select a.uuid from SOrg a where a.orgLevel = :orgLevel and a.orgType = '1'  order by orderNum")
    							.setParameter("orgLevel", orgLevel).list();
    		} else {
    			return new QueryCache("select uuid from SOrg where uuid = :orgId").setParameter("orgId", orgId).list();
    		}
    	} else if (IdsService.DEPT_ORG_TYPE.equals(orgType)) {
    		if (StringHelper.isEmpty(orgId)) {
    			return new QueryCache("select distinct b.deptId from SUserOrg b where b.orgId in (select a.uuid from SOrg a where a.orgLevel = :orgLevel and orgType = '1'  order by orderNum)")
    					.setParameter("orgLevel", orgLevel).list();
    		} else {
    			return new QueryCache("select distinct b.detpId from SUserOrg b where b.orgId = :orgId").setParameter("orgId", orgId).list();
    		}
    	} else {
			return new QueryCache("select uuid from SOrg order by orderNum").list();
    	}
    }
    
    @SuppressWarnings("unchecked")
	public static List<SOrg> listAllOrg(int orgLevel, String orgId, String orgType) {
    	List<SOrg> orgs = QueryCache.idToObj(SOrg.class, listAllOrgId(orgLevel, orgId, orgType));
    	return validateOrg(orgs);
    }
    
    @SuppressWarnings("unchecked")
    public static List<SOrg> syncOrgs(Date time) {
        StringBuffer sb = new StringBuffer(" select uuid from SOrg ");
        if (null != time) {
            sb.append(" where editDate >= :editDate ");
        }
        QueryCache qc = new QueryCache(sb.toString());
        if (null != time) {
            qc.setParameter("editDate", time);
        }
        List<String> ids = qc.list();
        if (ids == null) {
            return null;
        }
        return QueryCache.idToObj(SOrg.class, ids);
    }
    
    @SuppressWarnings("unchecked")
	private static <T> T validateOrg(T t) {
    	if(t instanceof List) {
    		List<SOrg> orgs = (List<SOrg>) t;
    		List<SOrg> orgList = null;
    		if(ListUtil.isLstNotEmpty(orgs)) {
    			orgList = new ArrayList<>();
    			for (SOrg user : orgs) {
    				if (Constants.NO.equals(user.getDelFlag()) && Constants.YES.equals(user.getOpenFlag())) {
    					orgList.add(user);
    				}
    			}
    		}
    		return (T) orgList;
    	}
    	if(t instanceof SOrg) {
    		SOrg org = (SOrg) t;
    		if (Constants.NO.equals(org.getDelFlag()) && Constants.YES.equals(org.getOpenFlag())) {
    			return t;
    		}
    	}
    	return null;
    }
	
}
